<script>
import { host } from '@/utils/conf'
import { isAuth, timeStamp, userLogin } from '@/utils/index'
export default {
  data () {
    return {
      openId: '',
      userInfo: {},
    }
  },
  methods: {
    async getOpenId (wxcode) {
      var open = await this.$request({
        url: '/GroupBuyBackendV1/getOpenId?js_code='+ wxcode
      })
      this.openId = open.openid
      // this.userInfo.openId = open.openid
      // this.userInfo.sessionKey = open.session_key
      wx.setStorageSync('openId', open.openid)
      if (this.openId) {
        this.getUserInfo()
      }
    },
    //设置用户信息
    async setUser (uInfo) {
      var that = this
      var user = {}
      var userInfo = wx.getStorageSync('userInfo')

      if (userInfo) {
        userInfo = JSON.parse(userInfo)
        if (userInfo && userInfo.name && userInfo.address && userInfo.mobile) {
          return
        }
      }
      
      user = await this.$request({
        url: '/GroupBuyBackendV1/getUserByOpenId/' + that.openId
      })
    
      if (user && user.userId) {
        var info = await this.$request({
          url: '/GroupBuyBackendV1/user/'+ user.userId
        })
        if (info.name && info.address && info.mobile) {
          var userData = {
            name: info.name,
            mobile: info.mobile,
            address: info.address
          }
        } else {
          user = await this.$request({
            url: '/GroupBuyBackendV1/user',
            type: 'POST',
            data: {
              openId: openId,
              userInfo: JSON.stringify(uInfo),
              dateTime: new Date().getTime()
            }
          })
        }
      } else {
        user = await this.$request({
          url: '/GroupBuyBackendV1/user',
          type: 'POST',
          data: {
            openId: openId,
            userInfo: JSON.stringify(uInfo),
            dateTime: new Date().getTime()
          }
        })
      }
     
      if (user.userId) {
        wx.setStorageSync('userId', user.userId)
        wx.setStorageSync('userIcon', uInfo.avatarUrl)
        wx.setStorageSync('userJson', JSON.stringify(uInfo))
      }
      if (typeof userData != 'undefined') {
        wx.setStorageSync('userInfo', JSON.stringify(userData))
      }
    },
    //获取微信信息
    getUserInfo () {
      var that = this
      wx.getSetting({
        success: function (res) {
          //已经授权
          if (res.authSetting['scope.userInfo']) {
            wx.getUserInfo({
              success (res) {
                that.setUser(res.userInfo)
              }
            })
          } else {
            wx.navigateTo({
              url: '/pages/login/main'
            })
          }
        }
      })
    },
    getInfo () {
      const that = this;

      if (this.openId) {
        that.getUserInfo()
      } else {
        wx.login({
          success (res) {
            if (res.code) {
              that.getOpenId(res.code)
            }
          },
          fail (e) {
            wx.showToast({
              icon: 'none',
              title: '登陆接口调用失败'
            })
          }
        })
      }
    },
    //初始化分享信息
    initShare () {
      wx.setStorageSync('shareTitle', '开团小助手')
      wx.setStorageSync('shareImg', '/static/img/index/background.jpg')
    }
  },
  mounted () {
    this.openId = wx.getStorageSync('openId')
    this.initShare()
    // userLogin()
  },
}
</script>

<style>
img {
  display: block;
}
.none, .hide {
  display: none !important;
}
page {
  background: #fafafa;
  height: 100%;
}

.btn-reset {
  padding: 0;
  background: rgba(0,0,0,0);
  line-height: auto;
  border-radius: 0;
  font-size: 24rpx;
  border: none;
  padding: 0
}
.btn-reset:after {
  border: none
}
@font-face {
  font-family: 'iconfont';  /* project id 711300 */
  src: url('//at.alicdn.com/t/font_711300_j15qkyyd2da.eot');
  src: url('//at.alicdn.com/t/font_711300_j15qkyyd2da.eot?#iefix') format('embedded-opentype'),
  url('//at.alicdn.com/t/font_711300_j15qkyyd2da.woff') format('woff'),
  url('//at.alicdn.com/t/font_711300_j15qkyyd2da.ttf') format('truetype'),
  url('//at.alicdn.com/t/font_711300_j15qkyyd2da.svg#iconfont') format('svg');
}
.iconfont{
  font-family: "iconfont"
}

</style>
